var Tank = require("Tank");
var Ball = require("Ball");

var Config = {
	INTERVAL: 50,
	ARENA_MARGIN: 30,
	WIDTH: 720,
	HEIGHT: 1080,
	BALL_SPEED: 300
};
cc.Config = Config;

cc.Class({
	extends: cc.Component,

	properties: {
		joinGameLayer: {
			default: null,
			type: cc.Node
		},
		tankName: {
			default: null,
			type: cc.EditBox
		},
		joinButton: {
			default: null,
			type: cc.Button
		},
		radioButton: {
			default: [],
			type: cc.Toggle
		},
		tankPrefab: {
			default: null,
			type: cc.Prefab
		},
		ballPrefab: {
			default: null,
			type: cc.Prefab
		},
		tankDeadPrefab: {
			default: null,
			type: cc.Prefab
		},
		arenaLayer: {
			default: null,
			type: cc.Node
		}
	},

	// use this for initialization
	onLoad: function () {
		this.initGame();
	},

	initGame: function () {
		this.tanks = {}; //Tanks (other than the local tank)
		this.balls = {};

		this.arenaId = 0;
		this.width = cc.Config.WIDTH;
		this.height = cc.Config.HEIGHT;
		this.socket = cc.Network.socket;
		this.selectedTank = 1;

		cc.find("Canvas/joystick").active = false;
		this.tankName.string = cc.Util.getName();
	},

	radioButtonClicked: function (toggle) {
		var index = this.radioButton.indexOf(toggle);
		// var title = "RadioButton";
		switch (index) {
			case 0:
				this.selectedTank = 1;
				break;
			case 1:
				this.selectedTank = 2;
				break;
			case 2:
				this.selectedTank = 3;
				break;
			default:
				break;
		}
		// cc.log(title);
	},

	joinGameClicked: function () {
		if (this.tankName.string != '') {
			cc.Network.init(this);
			cc.Network.joinGame(this.tankName.string, this.selectedTank);
			this.joinGameLayer.active = false;
			this.arenaLayer.active = true;
		}
	},


	///////////////////////////////////////game logic//////////////////////////////////////////////
	addTank: function (id, type, isLocal, x, y, hp) {
		var node = cc.instantiate(this.tankPrefab);
		node.getComponent('Tank').init(this, id, type, this.arenaId, isLocal, x, y, hp);
		node.parent = this.arenaLayer;

		if (isLocal) {
			this.localTank = node;
		} else {
			this.tanks[id] = node;
		}
	},

	removeTank: function (tank) {
		// tank.removeFromParent();
		tank.destroy();
	},

	deleteTank: function (tankId) {
		if (this.localTank && tankId == this.localTank.id) {
			this.localTank = null;
		} else {
			delete this.tanks[tankId];
		}
	},

	killTank: function (tank) {
		// if(tank == null) return;
		var tdata = tank.getComponent("Tank");
		tdata.dead = true;

		var node = cc.instantiate(this.tankDeadPrefab);
		node.x = tank.x;
		node.y = tank.y;
		node.parent = this.arenaLayer;
		setTimeout(function () {
			// node.removeFromParent();
			node.destroy();
		}, 0.6);

		this.removeTank(tank);
	},

	addBall: function (ball) {
		var node = cc.instantiate(this.ballPrefab);
		node.getComponent('Ball').init(this, ball.id, ball.ownerId, this.arenaId, ball.x, ball.y, ball.alpha);
		node.parent = this.arenaLayer;
		this.balls[ball.id] = node;
	},

	// called every frame, uncomment this function to activate update callback
	update: function (dt) {
		if (this.localTank) {
			var t = this.localTank.getComponent('Tank');
			if (this.tick == null) this.tick = 0;
			this.tick += dt;
			if (this.tick > 0.05) {
				this.tick -= 0.05;
				if (t.isMoving) {
					t.sendData();//send data to server about local tank 
				}
			}

			t.move();
		}
	},

	receiveData: function (serverData) {
		var game = this;
		var localTank = game.localTank;
		var localData = localTank ? localTank.getComponent("Tank") : null;
		serverData.tanks.forEach(function (serverTank) {

			//Update local tank stats
			if (localTank && serverTank.id == localData.id) {
				localData.hp = serverTank.hp;
				if (localData.hp <= 0) {
					game.killTank(localTank);
				}
			}

			//Update foreign tanks
			var found = false;
			for (var i in game.tanks) {
				var clientTank = game.tanks[i].getComponent('Tank');
				if (clientTank.id == serverTank.id) {
					clientTank.node.x = serverTank.x;
					clientTank.node.y = serverTank.y;
					clientTank.baseAngle = serverTank.baseAngle;
					clientTank.cannonAngle = serverTank.cannonAngle;
					clientTank.hp = serverTank.hp;
					if (clientTank.hp <= 0) {
						game.killTank(game.tanks[i]);
					}
					clientTank.refresh();
					found = true;
				}
			}
			if (!found &&
				(localTank == null || serverTank.id != localData.id)) {
				//I need to create it
				game.addTank(serverTank.id, serverTank.type, false, serverTank.x, serverTank.y, serverTank.hp);
			}
		});

		//update balls
		for (var i in serverData.balls) {
			var serverBall = serverData.balls[i];
			var ballNode = game.balls[serverBall.id];
			if (ballNode) {
				var ball = ballNode.getComponent('Ball');
				if (serverBall.exploding && !ball.exploding) {
					ball.exploding = true;
					ball.explode();
				}
			} else {
				game.addBall(serverBall);
			}
		}
	}
});
